#ifndef  __VECTOR__HPP
#define __VECTOR__HPP
#include <iostream>
#include <assert.h>

template <class T>
class Vector
{
public:
    //迭代器相关接口
    typedef T valuetype;
    typedef valuetype* iterator;
    typedef const valuetype* const_iterator;
    iterator begin() { return _start; }
    iterator end() { return _finish; }
    const_iterator begin() const { return _start; }
    const_iterator end() const { return _finish; }

    //默认成员函数
    Vector():_start(nullptr),_finish(nullptr),_endofstorage(nullptr){}



    //空间相关接口
    size_t size() const { return _finish - _start; }
    size_t capacity() const { return _endofstorage - _start; }
    bool empty() const {return _start == _finish; }
    void reserve(size_t n);
    void resize(size_t n,const valuetype& x = valuetype());

    //增删查改
    void push_back(const valuetype& x);
    void pop_back();

private:
    iterator _start;
    iterator _finish;
    iterator _endofstorage;
};

template <class T>
void Vector<T>::reserve(size_t n)
{
    size_t oldcapacity = capacity();

    if(n > oldcapacity)
    {
        size_t oldsize = size();

        iterator temp = new valuetype[n];

        for(size_t i = 0; i < oldsize; i++)//不用memecpy的原因是为了解决浅拷贝的问题
        {
            temp[i] = _start[i];
        }

        delete[] _start;

        _start = temp;
        _finish = _start + oldsize;
        _endofstorage = _start + n;
    }

}

template <class T>
void Vector<T>::resize(size_t n,const valuetype& x)
{
    // reserve(n);
    if(n > capacity())
    {
        while(_finish != _endofstorage)
        {
            *_finish = x;
            _finish++;
        }
    }

}

template <class T>
void Vector<T>::push_back(const valuetype& x)
{
    if(_endofstorage == _finish)
    {
        size_t newcapacity = capacity() == 0 ? 4 : capacity() * 2;
        reserve(newcapacity);
    }

    *_finish = x;
    _finish++;
}

template <class T>
void Vector<T>::pop_back()
{
    assert(_start != _finish);
    _finish--;
}

#endif // ! 

